package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author lvFF
 * @Date 2022/04/27/上午 11:41;
 * Description:文件的上传和下载
 */
@RestController
@RequestMapping("common")
@Slf4j
public class CommonController {
    @Value("${reggie.path}")
    private String BasePath;

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")          //file名字 和前端一致
    public R<String> upload(MultipartFile file) throws IOException {
        log.info("file----------{}", file);
        //判断文件地址是否存在
        File file1 = new File(BasePath);
        if (!file1.exists()) {
            //目录不存在 创建
            file1.exists();
        }

        //存在  不管理
        //获取原文件名
        String originalFilename = file.getOriginalFilename();
        log.info("originalFilename-----{}", originalFilename);
        //截取 。jpg文件后缀类型  string的截取 记住
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info("substring------------{}", substring);
        //使用UUID 编码文件前面前缀不重复，+ 后缀文件类型
        String basePath = UUID.randomUUID().toString() + substring;
        log.info("basePath-------------{}", basePath);
        //完成文件名字后 上传文件零时存储的位置
        file.transferTo(new File(BasePath + basePath));

        log.info("basePath-------{}", basePath);
        return R.success(basePath);

    }

    /**
     * 文件下载
     *
     * @param name
     */
    @GetMapping("download")
    public void download(String name, HttpServletResponse response) throws Exception {
       /* try {
            //输入流
            FileInputStream inputStream = new FileInputStream(new File(BasePath+name));
            //输出流
            ServletOutputStream outputStream = response.getOutputStream();

            //设置响应文件的类型
            response.setContentType("image/jpeg");


            int len = 0;
            byte[] bytes = new byte[1024*2];
            if ((len=inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }
            //关闭资源
            inputStream.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //输入流

        FileInputStream inputStream = new FileInputStream(new File(BasePath + name));
        //输出流
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(inputStream,outputStream);

    }
}
